home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalScrollBarUI.java < prev    next >
Text File  |  1998-06-30  |  10KB  |  405 lines

  1. /*
  2.  * @(#)MetalScrollBarUI.java    1.11 98/02/06
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import java.awt.Component;
  24. import java.awt.Container;
  25. import java.awt.LayoutManager;
  26. import java.awt.Adjustable;
  27. import java.awt.event.AdjustmentListener;
  28. import java.awt.event.AdjustmentEvent;
  29. import java.awt.event.ActionListener;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.MouseListener;
  32. import java.awt.event.MouseMotionListener;
  33. import java.awt.event.MouseAdapter;
  34. import java.awt.event.MouseEvent;
  35. import java.awt.Graphics;
  36. import java.awt.Dimension;
  37. import java.awt.Rectangle;
  38. import java.awt.Point;
  39. import java.awt.Insets;
  40. import java.awt.Color;
  41. import java.io.Serializable;
  42. import java.awt.IllegalComponentStateException;
  43.  
  44. import java.beans.*;
  45.  
  46. import com.sun.java.swing.*;
  47. import com.sun.java.swing.event.*;
  48.  
  49. import com.sun.java.swing.plaf.*;
  50. import com.sun.java.swing.plaf.basic.BasicScrollBarUI;
  51.  
  52.  
  53. /**
  54.  * Implementation of ScrollBarUI for the Metal Look and Feel
  55.  * <p>
  56.  * Warning: serialized objects of this class will not be compatible with
  57.  * future swing releases.  The current serialization support is appropriate
  58.  * for short term storage or RMI between Swing1.0 applications.  It will
  59.  * not be possible to load serialized Swing1.0 objects with future releases
  60.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  61.  * baseline for the serialized form of Swing objects.
  62.  *
  63.  * @version 1.11 02/06/98
  64.  * @author Jeff Shapiro
  65.  */
  66. public class MetalScrollBarUI extends BasicScrollBarUI
  67. {
  68.     private static Color shadowColor;
  69.     private static Color highlightColor;
  70.     private static Color thumbColor;
  71.     private static Color thumbShadow;
  72.     private static Color thumbHighlightColor;
  73.  
  74.     protected MetalBumps bumps;
  75.  
  76.     protected MetalScrollButton increaseButton;
  77.     protected MetalScrollButton decreaseButton;
  78.  
  79.     protected static int scrollBarWidth;
  80.  
  81.     protected ScrollBarListener scrollBarListener = new ScrollBarListener();
  82.  
  83.     public static final String FREE_STANDING_PROP = "JScrollBar.isFreeStanding";
  84.     protected boolean isFreeStanding = true;
  85.  
  86.     public static ComponentUI createUI( JComponent c )
  87.     {
  88.         return new MetalScrollBarUI();
  89.     }
  90.  
  91.     public void installUI( JComponent c )
  92.     {
  93.  
  94.     scrollBarWidth = ((Integer)(UIManager.get( "ScrollBar.width" ))).intValue();
  95.     if (c.getBackground() == null || c.getBackground() instanceof UIResource)
  96.         c.setBackground(UIManager.getColor("ScrollBar.background"));
  97.  
  98.         super.installUI( c );
  99.  
  100.     bumps = new MetalBumps( 10, 10, thumbHighlightColor, thumbShadow, thumbColor );
  101.  
  102.     c.addPropertyChangeListener( scrollBarListener );
  103.  
  104.     scrollBarListener.handlePropertyChange( c.getClientProperty( FREE_STANDING_PROP ) );
  105.     }
  106.  
  107.     public void uninstallUI( JComponent c )
  108.     {
  109.         super.uninstallUI( c );
  110.     c.removePropertyChangeListener( scrollBarListener );
  111.     }
  112.  
  113.     protected void configureScrollBarColors()
  114.     {
  115.         shadowColor         = UIManager.getColor("ScrollBar.shadow");
  116.         highlightColor      = UIManager.getColor("ScrollBar.highlight");
  117.         thumbColor          = UIManager.getColor("ScrollBar.thumb");
  118.         thumbShadow         = UIManager.getColor("ScrollBar.thumbShadow");
  119.         thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
  120.  
  121.     }
  122.  
  123.     public Dimension getPreferredSize( JComponent c )
  124.     {
  125.         if ( scrollbar.getOrientation() == JScrollBar.VERTICAL )
  126.     {
  127.         return new Dimension( scrollBarWidth, scrollBarWidth * 3 + 10 );
  128.         }
  129.     else  // Horizontal
  130.     {
  131.             return new Dimension( scrollBarWidth * 3 + 10, scrollBarWidth );
  132.         }
  133.     }
  134.  
  135.     /** Returns the view that represents the decrease view. 
  136.       */
  137.     protected JButton createDecreaseButton( int orientation )
  138.     {
  139.         decreaseButton = new MetalScrollButton( orientation, scrollBarWidth, isFreeStanding );
  140.     return decreaseButton;
  141.     }
  142.  
  143.     /** Returns the view that represents the increase view. */
  144.     protected JButton createIncreaseButton( int orientation )
  145.     {
  146.         increaseButton =  new MetalScrollButton( orientation, scrollBarWidth, isFreeStanding );
  147.     return increaseButton;
  148.     }
  149.  
  150.     protected void paintTrack( Graphics g, JComponent c, Rectangle trackBounds )
  151.     {
  152.         g.translate( trackBounds.x, trackBounds.y );
  153.  
  154.     if ( scrollbar.getOrientation() == JScrollBar.VERTICAL )
  155.     {
  156.         if ( !isFreeStanding ) {
  157.             trackBounds.width += 2;
  158.         }
  159.  
  160.         if ( c.isEnabled() ) {
  161.             g.setColor( shadowColor );
  162.         g.drawLine( 0, 0, 0, trackBounds.height - 1 );
  163.         g.drawLine( trackBounds.width - 2, 0, trackBounds.width - 2, trackBounds.height - 1 );
  164.         g.drawLine( 2, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1);
  165.         g.drawLine( 2, 0, trackBounds.width - 2, 0 );
  166.  
  167.         g.setColor( highlightColor );
  168.         g.drawLine( 1, 1, 1, trackBounds.height - 2 );
  169.         g.drawLine( 1, 1, trackBounds.width - 3, 1 );
  170.         g.drawLine( trackBounds.width - 1, 0, trackBounds.width - 1, trackBounds.height - 1 );
  171.         } else {
  172.         MetalUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height );
  173.         }
  174.  
  175.         if ( !isFreeStanding ) {
  176.             trackBounds.width -= 2;
  177.         }
  178.     }
  179.     else  // HORIZONTAL
  180.     {
  181.         if ( !isFreeStanding ) {
  182.             trackBounds.height += 2;
  183.         }
  184.  
  185.         if ( c.isEnabled() ) {
  186.             g.setColor( shadowColor );
  187.         g.drawLine( 0, 0, trackBounds.width - 1, 0 );  // top
  188.         g.drawLine( 0, 2, 0, trackBounds.height - 2 ); // left
  189.         g.drawLine( 0, trackBounds.height - 2, trackBounds.width - 1, trackBounds.height - 2 ); // bottom
  190.         g.drawLine( trackBounds.width - 1, 2, trackBounds.width - 1, trackBounds.height - 1 ); // right
  191.  
  192.         g.setColor( highlightColor );
  193.         g.drawLine( 1, 1, trackBounds.width - 2, 1 );  // top
  194.         g.drawLine( 1, 1, 1, trackBounds.height - 3 ); // left
  195.         g.drawLine( 0, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1 ); // bottom
  196.         } else {
  197.             MetalUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height );
  198.         }
  199.  
  200.         if ( !isFreeStanding ) {
  201.             trackBounds.height -= 2;
  202.         }
  203.     }
  204.  
  205.         g.translate( -trackBounds.x, -trackBounds.y );
  206.     }
  207.  
  208.     protected void paintThumb( Graphics g, JComponent c, Rectangle thumbBounds )
  209.     {
  210.         if (!c.isEnabled()) {
  211.         return;
  212.     }
  213.  
  214.         g.translate( thumbBounds.x, thumbBounds.y );
  215.  
  216.     if ( scrollbar.getOrientation() == JScrollBar.VERTICAL )
  217.     {
  218.         if ( !isFreeStanding ) {
  219.             thumbBounds.width += 2;
  220.         }
  221.  
  222.         g.setColor( thumbColor );
  223.         g.fillRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 );
  224.  
  225.         g.setColor( thumbShadow );
  226.         g.drawRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 );
  227.     
  228.         g.setColor( thumbHighlightColor );
  229.         g.drawLine( 1, 1, thumbBounds.width - 3, 1 );
  230.         g.drawLine( 1, 1, 1, thumbBounds.height - 2 );
  231.  
  232.         bumps.setBumpArea( thumbBounds.width - 6, thumbBounds.height - 7 );
  233.         bumps.paintIcon( c, g, 3, 4 );
  234.  
  235.         if ( !isFreeStanding ) {
  236.             thumbBounds.width -= 2;
  237.         }
  238.     }
  239.     else  // HORIZONTAL
  240.     {
  241.         if ( !isFreeStanding ) {
  242.             thumbBounds.height += 2;
  243.         }
  244.  
  245.         g.setColor( thumbColor );
  246.         g.fillRect( 0, 0, thumbBounds.width - 1, thumbBounds.height - 2 );
  247.  
  248.         g.setColor( thumbShadow );
  249.         g.drawRect( 0, 0, thumbBounds.width - 1, thumbBounds.height - 2 );
  250.  
  251.         g.setColor( thumbHighlightColor );
  252.         g.drawLine( 1, 1, thumbBounds.width - 3, 1 );
  253.         g.drawLine( 1, 1, 1, thumbBounds.height - 3 );
  254.  
  255.         bumps.setBumpArea( thumbBounds.width - 7, thumbBounds.height - 6 );
  256.         bumps.paintIcon( c, g, 4, 3 );
  257.  
  258.         if ( !isFreeStanding ) {
  259.             thumbBounds.height -= 2;
  260.         }
  261.     }
  262.  
  263.         g.translate( -thumbBounds.x, -thumbBounds.y );
  264.     }
  265.  
  266.     protected Dimension getMinimumThumbSize()
  267.     {
  268.         return new Dimension( scrollBarWidth, scrollBarWidth );
  269.     }        
  270.  
  271.     class ScrollBarListener implements PropertyChangeListener, Serializable
  272.     {
  273.         public void propertyChange(PropertyChangeEvent e)
  274.     {
  275.         String name = e.getPropertyName();
  276.         if ( name.equals( FREE_STANDING_PROP ) )
  277.         {
  278.             handlePropertyChange( e.getNewValue() );
  279.         }
  280.     }
  281.  
  282.         public void handlePropertyChange( Object newValue )
  283.     {
  284.         if ( newValue != null )
  285.         {
  286.             boolean temp = ((Boolean)newValue).booleanValue();
  287.         boolean becameFlush = temp == false && isFreeStanding == true;
  288.         boolean becameNormal = temp == true && isFreeStanding == false;
  289.         
  290.         isFreeStanding = temp;
  291.  
  292.         if ( becameFlush ) {
  293.             toFlush();
  294.         }
  295.         else if ( becameNormal ) {
  296.             toFreeStanding();
  297.         }
  298.         }
  299.         else
  300.         {
  301.  
  302.             if ( !isFreeStanding ) {
  303.             isFreeStanding = true;
  304.             toFreeStanding();
  305.         }
  306.         
  307.         // This commented-out block is used for testing flush scrollbars.
  308. /*
  309.             if ( isFreeStanding ) {
  310.             isFreeStanding = false;
  311.             toFlush();
  312.         }
  313. */
  314.         }
  315.         
  316.         if ( increaseButton != null )
  317.         {
  318.             increaseButton.setFreeStanding( isFreeStanding );
  319.         }
  320.         if ( decreaseButton != null )
  321.         {
  322.             decreaseButton.setFreeStanding( isFreeStanding );
  323.         }        
  324.     }
  325.  
  326.         protected void toFlush() {
  327.         scrollBarWidth -= 2;
  328.         }
  329.  
  330.         protected void toFreeStanding() {
  331.         scrollBarWidth += 2;
  332.         }
  333.     } // end class ScrollBarListener
  334. }
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.